﻿function FilterN(tb, KeyEvent, allowNegative) {
    var nCharCode = 0;

    if (typeof (KeyEvent.which) != 'undefined')
        nCharCode = KeyEvent.which;
    else if (typeof (KeyEvent.keyCode) != 'undefined')
        nCharCode = KeyEvent.keyCode;

    switch (nCharCode) {
        case 0:
        case 8:
        case 9:
        case 13:
            return true;
            break;
        case 45: //minus
            if (allowNegative) {
                HandleMinus(tb);
            }
            return false;
            break;
        case 46:
        case 44:
            HandlePoint(tb);
            return false;
            break;
        default:
            if (nCharCode > 57 || nCharCode < 48)
                return false;
            else
                return true;
            break;
    }
}

function HandleMinus(tb) {
    if (tb.value.indexOf('-') < 0)
        tb.value = "-" + tb.value;
}

function HandlePoint(tb) {
    if (tb.value.indexOf(',') < 0)
        tb.value += ",";
}

function FilterNumeric(tb, KeyEvent, allowNegative) {
    var r = FilterN(tb, KeyEvent, allowNegative);
    return r;
}

function ReStyle(tb, KeyEvent, allowNegative, positiveText, negativeText, textControl) {
    var index = tb.value.indexOf('-');
    var isNeg = (index >= 0);

    if (isNeg && (allowNegative == false)) {
        tb.value = tb.value.replace('-', '');
    }
    else {
        var t = document.getElementById(textControl);
        if (isNeg) {
            tb.value = '-' + tb.value.replace('-', '');
            tb.style.backgroundColor = "#ff8080";
            if (t)
                t.innerHTML = negativeText;
        }
        else {
            tb.style.backgroundColor = "white";
            if (t)
                t.innerHTML = positiveText;
        }
    }
}

